package org.example;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ParseBarcodes {
    public static JSONArray generateBarcodesAndAmount(String[] barcodesArray) {
        JSONArray barcodeAmountArray = new JSONArray();
        for(String barcode : barcodesArray) {
            String barcodeAmount;
            String barcodeName;
            JSONObject jsonObject = new JSONObject();
            if(!barcode.contains("-")) {
                barcodeAmount = "1";
                barcodeName = barcode;
            }else{
                barcodeName = barcode.substring(0, barcode.indexOf("-"));
                barcodeAmount = barcode.substring(barcodeName.length()+1);
            }
            jsonObject.put("barcode" , barcodeName);
            jsonObject.put("amount" , barcodeAmount);
            barcodeAmountArray.add(jsonObject);
        }
        return barcodeAmountArray;
    }
}
